<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>动态生成表格</title>
    <style>
      table {
        border: 1px solid black;
      }
    </style>
  </head>
  <body>
    <table>
      <thead>
        <th>角色</th>
        <th>颜色</th>
        <th>操作</th>
      </thead>
      <tbody></tbody>
    </table>
    <script>
      var datas = [
        { name: "恐龙", color: "green" },
        { name: "鸡", color: "yellow" },
      ];
      var tbody = document.querySelector("tbody");
      for (let i = 0; i < datas.length; i++) {
        var tr = document.createElement("tr");
        tbody.appendChild(tr);
        for (var k in datas[i]) {
          var td = document.createElement("td");
          td.innerHTML = datas[i][k];
          tr.appendChild(td);
        }
        var td = document.createElement("td");
        td.innerHTML = '<a href="javascript:;">删除</a>';
        tr.appendChild(td);
        var delet = document.querySelectorAll("a");
        for (let j = 0; j < delet.length; j++) {
          delet[j].onclick = function () {
            tbody.removeChild(this.parentNode.parentNode);
          };
        }
      }
    </script>
  </body>
</html>
